﻿using AspNetCoreWebApi.Module;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace AspNetCoreWebApi.Controllers
{
    [Route("api/IndexHtml")]
    [ApiController]
    public class IndexHtmlController : ControllerBase
    {
        [HttpGet]
        public IActionResult GetIndexHtml()
        {
            //var data = new FileStream(Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "A2CDA41D-8572-45c2-A2FF-2A245C07B15D.png"), FileMode.OpenOrCreate,FileAccess.ReadWrite);
            //Response.ContentType = "application/octet-stream";
            //await data.CopyToAsync(Response.Body);
            //await Response.CompleteAsync();
            //data.Close();
            // 1、html文件返回

            //var data = new FileStream(Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "A2CDA41D-8572-45c2-A2FF-2A245C07B15D.png"), FileMode.OpenOrCreate, FileAccess.ReadWrite);
            //Response.ContentType = "image/png";
            //await data.CopyToAsync(Response.Body);
            //await Response.CompleteAsync();
            //data.Close();

            return File(new FileStream(Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "A2CDA41D-8572-45c2-A2FF-2A245C07B15D.png"), FileMode.Open),
                    "image/png");

        }

        [HttpGet]
        [Route("String")]
        public IActionResult GetString()
        {
            // 1、html文件返回
            return Ok("Tony讲webapi");
        }

        [HttpGet]
        [Route("Object")]
        public IActionResult GetObject()
        {
            // 1、html文件返回
            Product product = new Product();
            product.Id = 1;
            product.Name = "电脑";
            return Ok(product);
        }

        /// <summary>
        /// 获取Cookie
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Route("Cookie")]
        public IActionResult GetCookie()
        {
            // 1、获取Cookie值 : HttpContext  HttpRequest
            string value = Request.Cookies["Tony"];

            return Ok(value);
        }

        /// <summary>
        /// 获取Cookie
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Route("Cookie")]
        public IActionResult PostCookie()
        {
            // 1、获取Cookie值 : HttpContext  HttpRequest
            Response.Cookies.Append("zhang", "所有客户端共享");

            return Ok("Cookie设置成功");
        }

        /// <summary>
        /// 获取Header
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Route("Header")]
        public IActionResult GetHeader()
        {
            // 1、获取Header值 : HttpContext  HttpRequest
            string value = Request.Headers["Z"];

            return Ok(value);
        }

        /// <summary>
        /// 设置响应头Header
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Route("Header")]
        public IActionResult PostHeader()
        {
            // 1、设置响应Header头 : HttpContext  HttpRequest
            Response.Headers.Append("zhang-x", "123444");

            return Ok("Header设置成功");
        }
    }
}
